use cmsis_rtos2_rs::static_mem::{Task, Thread};
use crate::app;
use super::{driver::cmd_can, service::{can_recv, dispatcher::DISPATCHER}};

#[cfg(feature = "cp_drop")]
mod cmd_uart_task;

mod handler_task;
mod status_task;

pub use handler_task::post_request;
pub use handler_task::get_run_flag;

pub unsafe fn start() {
    APP_THREAD.spawn(&AppTask);
}

struct AppTask;
static APP_THREAD: Thread<2048> = Thread::uninit();

fn init_all() {
    unsafe { 
        DISPATCHER.init_once(); 
    }
    cmd_can::init();

    #[cfg(feature = "cp_drop")]
    app::driver::cmd_uart::init();

    #[cfg(feature = "cp_drop")]
    app::drop::init_self_driver();

    #[cfg(feature = "cp_cargo")]
    app::cargo::init_self_driver();

    handler_task::start();
    
    #[cfg(feature = "cp_drop")]
    cmd_uart_task::start();

    status_task::start();
}

impl Task for AppTask {

    fn run(&self) {
        init_all();
        can_recv::can_recv_loop();
    }
}





